<!--
 * @author  dumeng
 调查问卷-设置单选题
-->
<template>
  <div class="question-danxuan">
    <el-form
      ref="danxuanform"
      :model="baseform"
      :rules="baserules"
      class="dataForm nomt"
      label-position="right"
      label-width="140px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="题干" prop="stem">
            <el-input
              v-model.trim="baseform.stem"
              :rows="2"
              type="textarea"
              placeholder="题干这里填写题目描述"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            v-for="(answer, index) in baseform.opts"
            :key="index"
            :label="`选项${index+1}`"
            required
            class="options"
          >
            <el-input
              v-model.trim="baseform.opts[index]"
              placeholder="请输入选项内容"
              @input="changeText($event,index)"
            />
            <span class="remove-option" @click.prevent="removeOption(index)">
              <i class="el-icon-delete" />
            </span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-button icon="el-icon-circle-plus" class="add-optiopn" @click="addOption">添加选项</el-button>
        </el-col>
        <el-col :span="24">
          <el-form-item label="允许填写其他答案" prop="isOther">
            <el-radio-group v-model="baseform.isOther">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否必答" prop="isMust">
            <el-radio-group v-model="baseform.isMust">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="排序" prop="sort">
            <el-input v-model.trim="baseform.sort" placeholder="请输入排序号" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { number } from '@/utils/validate'
export default {
  name: 'Danxuan',
  props: {
    type: {
      type: String,
      default: '10'
    },
    getData: {
      type: Object,
      default: () => { }
    }
  },
  data() {
    return {
      baseform: {
        stem: '',
        opts: ['', '', '', ''],
        isMust: '1',
        isOther: '1',
        sort: ''
      },
      baserules: {
        stem: [
          { required: true, message: '请输入题干', trigger: 'blur' }
        ],
        opts: [
          { required: true, message: '请输入备选答案', trigger: 'blur' }
        ],
        isMust: [
          { required: true, message: '请选择是否必答', trigger: 'blur' }
        ],
        isOther: [
          { required: true, message: '请选择是否允许填写其他答案', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序号', trigger: 'blur' },
          { validator: number, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.getData && this.getData.id) {
      try {
        const answer = JSON.parse(this.getData.answer)
        this.$set(this.baseform, 'stem', this.getData.stem)
        this.$set(this.baseform, 'sort', this.getData.sort)
        this.$set(this.baseform, 'isMust', this.getData.isMust)
        this.$set(this.baseform, 'isOther', this.getData.isOther)
        this.$set(this.baseform, 'opts', answer.opts)
      } catch (error) {
        this.$message({
          type: 'error',
          message: '参数异常，请重试!',
          onClose: () => {
            this.$router.back()
          }
        })
      }
    }
  },
  methods: {
    changeText(e, index) {
      if (e === '其他') {
        this.baseform.opts[index] = '其 他'
      }
    },
    /**
     * 删除指定选项
     * @param i
     */
    removeOption(i) {
      this.baseform.opts.splice(i, 1)
    },
    /**
     * 添加选项
     */
    addOption() {
      this.baseform.opts.push('')
    },
    /**
     * 判断数据中是否含有相同项
		 * @param array
		 * @returns {number}
		 * @constructor
		 */
    checkArrayElement(arr) {
      const array = [...[], ...arr]
      array.sort() // 数组排序
      var reNum = 0 // 返回结果
      // 遍历整个数组对象
      for (var i = 0; i < array.length; i++) {
        // 跳过最后一个元素的比较
        if (i + 1 === array.length) {
          continue
        }
        // 判断相邻的元素是否相同
        if (array[i] === array[i + 1]) {
          reNum += 1
        }
      }
      return reNum
    },
    getPostData() {
      let params = ''
      if (!this.baseform.opts.some(item => item)) {
        this.$message({
          message: '请填写选项内容！',
          type: 'warning'
        })
        return false
      }
      if (this.checkArrayElement(this.baseform.opts) > 0) {
        this.$message({
          message: '选项内容不能相同！',
          type: 'warning'
        })
        return false
      }
      this.$refs.danxuanform.validate(valid => {
        if (valid) {
          const answer = {
            type: this.type,
            opts: this.baseform.opts
          }
          params = {
            answer: JSON.stringify(answer),
            stem: this.baseform.stem,
            isMust: this.baseform.isMust,
            isOther: this.baseform.isOther,
            sort: this.baseform.sort
          }
        }
      })
      return params
    }
  }
}
</script>

<style lang="scss">
.question-danxuan {
  .el-radio-group {
    width: 100%;
  }
  .options .el-form-item__content {
    display: flex;
    align-items: center;
    .el-input {
      margin-right: 10px;
    }
  }
  .option-rightasnwer {
    display: inline-block;
    flex-shrink: 0;
    font-size: 14px;
    color: green;
    width: 90px;
    margin-right: 20px;
  }
  .order-fixed {
    margin: 10px 0;
    padding-left: 100px;
  }
  .remove-option {
    cursor: pointer;
  }
  .add-optiopn {
    margin-left: 100px;
    margin-bottom: 10px;
  }
  .tip {
    padding-left: 100px;
    padding-bottom: 10px;
    color: red;
  }
}
</style>
